package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.view.JasperViewer;
import Messages.MessageDeleteBusinessMenu;

import Messages.MessageShowBusinessMenus;
import Messages.MessageShowBusinessMenusReply;
/**
 * Class for creation right side panel for control of selected business menu
 * @author Andrey,Igor
 *
 */
public class PanelFunctionBusinessMenuThis  extends PanelFunctionsEmpty{

	private static final long serialVersionUID = 1L;
	private MainMenu mainMenu;
	
	private Vector<String> businessMenu;
	private String IDSupp;
	
	private JButton button_editMenu;
	private JButton button_DeleteMenu;
	private JButton button_new_menu;
	private JButton button_dohotEmployee;
	private JButton button_view_menu;
	private JButton button_print_menu;
	
	private Connection connection;
	
	private String flag="business";
	private static String password = "Braude"; 
	private static String username = "root";
	
	
public PanelFunctionBusinessMenuThis(MainMenu mm,Vector <String> menu)
{
	super();
	this.businessMenu=menu;
	this.mainMenu=mm;
	this.add(get_button_BuildNewMenu());
	this.add(get_button_editMenu());
	this.add(get_button_DeleteMenu(businessMenu));
	//this.add(get_button_ViewMenu());
	this.add(get_button_PrintMenu());

	
	
	}
	
/**
 * Method for creation JButton button_new_menu that opens window for creation new business menu(PfAddNewBusinessMenu)
 * @return button_new_menu
 */
private JButton get_button_BuildNewMenu() 
{
	if (button_new_menu == null) 
	{			
		button_new_menu=new JButton("בניית תפריט עסקי חדש");  			 
		button_new_menu.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button_new_menu.setPreferredSize(new Dimension(220, 30));
		
		button_new_menu.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
				mainMenu.getContentPane().add(new PfAddNewBusinessMenu(mainMenu), BorderLayout.CENTER);	
				mainMenu.getContentPane().add(new PanelFunctionEmptyWithoutKeyboard(mainMenu), BorderLayout.EAST);
					
				mainMenu.getContentPane().validate();					
			}
		});
	}
	return button_new_menu;
}





	
/**
 * Method for creation JButton 	button_editMenu that opens window for updating selected business menu(PfUpdateBusinessMenu)
 * @return button_editMenu
 */
private JButton get_button_editMenu() 
	{
		if (button_editMenu == null) 
		{			
			button_editMenu=new JButton(" עדכון תפריט");  			 
			button_editMenu.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_editMenu.setPreferredSize(new Dimension(220, 30));
			
			button_editMenu.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
					mainMenu.getContentPane().add(new PfUpdateBusinessMenu(mainMenu,businessMenu), BorderLayout.CENTER);	
					mainMenu.getContentPane().add(new PanelFunctionEmptyWithoutKeyboard(mainMenu), BorderLayout.EAST);
					mainMenu.getContentPane().validate();						
				}
			});
		}
		return button_editMenu;
	}
	
	
	
/**
 * Method for creation JButton 	button_DeleteMenu that deletes selected business menu
 * @param main 
 * @return button_DeleteMenu
 */
private JButton get_button_DeleteMenu(final Vector<String> main) 
	{
		if (button_DeleteMenu == null) 
		{			
			button_DeleteMenu=new JButton("מחיקת תפריט");  			 
			button_DeleteMenu.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_DeleteMenu.setPreferredSize(new Dimension(220, 30));
			
			button_DeleteMenu.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					
					
					int n = JOptionPane.showConfirmDialog(
						    null,
						    "    אתה בטוח?",
						    "    בקשת אישור  ",
						    JOptionPane.YES_NO_OPTION);
					 					
					 if(n!=1){
                        	MessageDeleteBusinessMenu msgDelMenu=new MessageDeleteBusinessMenu (businessMenu.get(0));
                       	try {
							mainMenu.ms.client.sendToServer(msgDelMenu);
							mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
							MessageShowBusinessMenus msgShowMenu=new MessageShowBusinessMenus( );
							mainMenu.ms.client.sendToServer(msgShowMenu);
							MessageShowBusinessMenusReply resultMenu=(MessageShowBusinessMenusReply)mainMenu.ms.client.GetMessage();
							mainMenu.getContentPane().add(new PfSearch_business_menu(mainMenu,resultMenu.getResultMenu()), BorderLayout.CENTER);	
							mainMenu.getContentPane().validate();
						}
						
						catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
												
						}
								
  				}
					 }
			});
		}
		return button_DeleteMenu;
	}
/**
 * Method for creation JButton button_view_menu that opens window with all dishes of selected business menu(PfShow_all_recipes_in_this_business_menu)
 * @return button_view_menu
 */
/*private JButton get_button_ViewMenu() 
{
	if (button_view_menu == null) 
	{			
		button_view_menu=new JButton("לראות תפריט");  			 
		button_view_menu.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button_view_menu.setPreferredSize(new Dimension(220, 30));
		button_view_menu.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				MessageGetRecipesMenuThis msgDelMenu=new MessageGetRecipesMenuThis (businessMenu.get(0),flag);
			 	
				try {
					mainMenu.ms.client.sendToServer(msgDelMenu);
					MessageGetRecipesMenuThisReply resultMenu=(MessageGetRecipesMenuThisReply)mainMenu.ms.client.GetMessage();
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().add(new PfShow_all_recipes_in_this_business_menu(mainMenu,resultMenu.getResultRecipes()), BorderLayout.CENTER);	
					mainMenu.getContentPane().add(new PanelFunctionMenuListAllBusMenu(mainMenu), BorderLayout.EAST);
					mainMenu.getContentPane().validate();
				}
				
				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
										
				}	
						
			}
		});
	}
	return button_view_menu;
}*/

private JButton get_button_PrintMenu() 
{
	
	if (button_print_menu == null) 
	{			
		button_print_menu=new JButton("להדפיס תפריט");  			 
		button_print_menu.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button_print_menu.setPreferredSize(new Dimension(220, 30));
		button_print_menu.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				Vector<String> arr = new Vector<String>() ;
				String iname="vasek";
				arr.add( iname);
				try 
				{
					Class.forName("com.mysql.jdbc.Driver").newInstance();
				} catch (Exception ex) {/* handle the error*/}
		    
				try 
				{
					connection = DriverManager.getConnection("jdbc:mysql://localhost/roberg?useUnicode=true&characterEncoding=utf-8",username,password);
				} catch (SQLException ex) 
		 	    	{/* handle any errors*/
					
		        }
				HashMap hm = new HashMap();
				//JRBeanCollectionDataSource reportData = new JRBeanCollectionDataSource(	arr);
				try {
					   JasperPrint print;
					
						print = JasperFillManager.fillReport("Reports/Orders/try.jasper", hm,connection );
						JasperViewer.viewReport(print);
					
					
					// JRExporter exporter = new JRPdfExporter();
					// Configure the exporter (set output file name and print object)
			            //exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "Reports/Orders/try.pdf");
			          //  exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
			 
			            // Export the PDF file
			          //  exporter.exportReport();
				
				} 
				
				
				
				catch (JRException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
						
			}
		});
	}
	return button_print_menu;
}




}



